//
//  util.h
//  base
//
//  Created by Demi on 17/03/2017.
//  Copyright © 2017 Azazie. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "TXFire.h"

NS_ASSUME_NONNULL_BEGIN

UIKIT_EXTERN NSString * _Nullable BasePathForResource(NSString *_Nonnull name, NSString *_Nullable ext);
UIKIT_EXTERN UIImage *_Nullable BaseImageWithNamed(NSString *name);

#ifndef main_thread_safe
#define main_thread_safe(block) \
    if (is_main_thread) \
    { \
        block(); \
    } \
    else \
    { \
        dispatch_async(dispatch_get_main_queue(), block);\
    }
#endif

// 是否是iPhone X系列
#define IS_iPhoneX       ([UIScreen instancesRespondToSelector:@selector(currentMode)] ?\
(\
CGSizeEqualToSize(CGSizeMake(375, 812),[UIScreen mainScreen].bounds.size)\
||\
CGSizeEqualToSize(CGSizeMake(812, 375),[UIScreen mainScreen].bounds.size)\
||\
CGSizeEqualToSize(CGSizeMake(414, 896),[UIScreen mainScreen].bounds.size)\
||\
CGSizeEqualToSize(CGSizeMake(896, 414),[UIScreen mainScreen].bounds.size))\
:\
NO)

#ifndef IS_AZAZIE
#define IS_AZAZIE \
[[NSBundle.mainBundle.bundleIdentifier componentsSeparatedByString:@"."][1] isEqualToString:@"azazie"]
#endif

#ifndef IS_LOVEPROM
#define IS_LOVEPROM \
[[NSBundle.mainBundle.bundleIdentifier componentsSeparatedByString:@"."][1] isEqualToString:@"loveprom"]
#endif


#pragma mark ——————————线程安全——————————
static inline void __unused safe_main_thread(dispatch_block_t block)
{
    if (is_main_thread)
    {
        block();
    }
    else
    {
        dispatch_async(dispatch_get_main_queue(), block);
    }
}

#pragma mark ——————————空对象判断——————————
static inline BOOL StringHasValue(NSString *string) {
    if (![string isKindOfClass:NSString.class]) {
        return NO;
    }
    if (string == nil || string == NULL) {
        return NO;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return NO;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return NO;
    }
    return YES;
}

static inline BOOL DictionaryHasValue(NSDictionary *dic) {
    if (![dic isKindOfClass:NSDictionary.class]) {
        return NO;
    }
    if (dic == nil || dic == NULL) {
        return NO;
    }
    if ([dic isKindOfClass:[NSNull class]]) {
        return NO;
    }
    if (!dic.count) {
        return NO;
    }
    return YES;
}

static inline BOOL ArrayHasValue(NSArray *arr) {
    if (![arr isKindOfClass:NSArray.class]) {
        return NO;
    }
    if (arr == nil || arr == NULL) {
        return NO;
    }
    if ([arr isKindOfClass:[NSNull class]]) {
        return NO;
    }
    if (arr.count==0) {
        return NO;
    }
    return YES;
}

#pragma mark ——————————URL安全——————————
static inline NSURL *URLWithEncoding(NSString *s)
{
    NSString *url = [s tx_stringByEncodingURL];
    if (url)
    {
        return [NSURL URLWithString:url];
    }
    return nil;
}

static inline NSURL *URLWithDecoding(NSString *s)
{
    NSString *url = [s tx_stringByDecodingURL];
    if (url)
    {
        return [NSURL URLWithString:url];
    }
    return nil;
}

#pragma mark ——————————边界判断——————————
typedef NS_OPTIONS(NSUInteger, CONFINE_LIMIT_TYPE) {
    CONFINE_LIMIT_TYPE_UPPER = 1 << 0,
    CONFINE_LIMIT_TYPE_LOWER = 1 << 1,
    CONFINE_LIMIT_TYPE_NOPE  = 1 << 2,
};

static inline BOOL confineto(float a, float b, float value, CONFINE_LIMIT_TYPE type) {
    float range_max = MAX(a, b);
    float range_min = MIN(a, b);
    if (type & CONFINE_LIMIT_TYPE_NOPE) {
        return value < range_max && value > range_min;
    } else {
        BOOL lower_pass = (value > range_min);
        BOOL upper_pass = (value < range_max);
        if ((type & CONFINE_LIMIT_TYPE_UPPER) && !lower_pass) return NO;
        if ((type & CONFINE_LIMIT_TYPE_LOWER) && !upper_pass) return NO;
        return YES;
    }
}

#pragma mark ——————————尺寸自适应 相对大小———————————
static inline CGSize CGSizeScaleAspectToHeight(CGSize size, CGFloat height)
{
    return CGSizeMake(floor(height * size.width / size.height), height);
}

static inline CGSize CGSizeScaleAspectToWidth(CGSize size, CGFloat width)
{
    return CGSizeMake(width, floor(width * size.height / size.width));
}

NS_ASSUME_NONNULL_END
